package com.edu.zju.scs.panacea.controller.plan;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Industry;
import com.edu.zju.scs.panacea.entity.plan.Qualification;
import com.edu.zju.scs.panacea.entity.plan.Seller;
import com.edu.zju.scs.panacea.entity.query.QualificationQuery;
import com.edu.zju.scs.panacea.mapper.plan.QualificationMapper;
import com.edu.zju.scs.panacea.service.plan.QualificationService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.Model;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-30 17:32
 */
@Controller
@EnableAutoConfiguration
public class QualificationController {

    @Autowired
    private QualificationService qualificationService;

    @Autowired
    private QualificationMapper qualificationMapper;

    @ResponseBody
    @ApiOperation(value="根据id查询某个资质", notes="根据id查询某个资质")
    @RequestMapping(value = "/qualification/{id}", method = RequestMethod.GET)
    public BaseResult getQualification(@PathVariable Long id) {
        return qualificationService.getQualification(id);
    }

    @ResponseBody
    @ApiOperation(value="查询某商家在某行业的资质状况", notes="查询某商家在某行业的资质状况")
    @RequestMapping(value = "/qualification/industry", method = RequestMethod.POST)
    public BaseResult getQualificationByIndustry(
            @ApiParam("行业ID") @RequestParam(value = "industryId") Long industryId,
            HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        QualificationQuery qualificationQuery = new QualificationQuery();
        qualificationQuery.setSellerId(seller.getId());
        qualificationQuery.setIndustryId(industryId);
        return qualificationService.getIndustryQualiBySeller(qualificationQuery);
    }

    @ResponseBody
    @ApiOperation(value="查询某商家的资质状况", notes="查询某商家的资质状况")
    @RequestMapping(value = "/qualification/seller", method = RequestMethod.GET)
    public BaseResult getQualificationBySeller(HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        QualificationQuery qualificationQuery = new QualificationQuery();
        qualificationQuery.setSellerId(seller.getId());
        return qualificationService.getQualificationBySeller(qualificationQuery);
    }

    @ResponseBody
    @ApiOperation(value="查询所有商家资质状况", notes="查询所有商家资质状况")
    @RequestMapping(value = "/qualification/seller/all", method = RequestMethod.GET)
    public BaseResult getAllQualification() {
        return qualificationService.getAllQualification();
    }

    @ResponseBody
    @ApiOperation(value="添加资质", notes="添加资质")
    @RequestMapping(value = "/qualification/new", method = RequestMethod.POST)
    public BaseResult addQualification(
            @ApiParam("行业ID") @RequestParam(value = "industry") Long industry,
            @ApiParam("资质名称") @RequestParam(value = "name") String name,
            @ApiParam("资质URL") @RequestParam(value = "url") String url,
            HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        Qualification qualification = new Qualification();
        qualification.setSellerId(seller.getId());
        qualification.setIndustryId(industry);
        qualification.setName(name);
        qualification.setUrl(url);
        qualification.setState(0);
        return qualificationService.addQualification(qualification);
    }

    //处理文件上传
    @ResponseBody
    @ApiOperation(value="处理文件上传", notes="处理文件上传")
    @RequestMapping(value = "/qualification/uploadimg", method = RequestMethod.POST)
    public BaseResult uploadImg(
            @RequestParam(value = "qualificationFile") MultipartFile file,
            @RequestParam(value = "industry") Long industry,
            @RequestParam(value = "name") String name,
            HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        return qualificationService.uploadImg(file, seller.getId(), industry, name);
    }

    //查看上传文件
    @ResponseBody
    @RequestMapping(value = "/qualificationPreview", method = RequestMethod.GET)

    public String previewImg(@RequestParam(value = "id") Long id,  HttpServletRequest request, HttpServletResponse response) {
        try {
            Qualification qualification = qualificationMapper.selectQualification(id);
            //读取本地图片输入流
            FileInputStream inputStream = new FileInputStream(qualification.getUrl());
            int i = inputStream.available();
            //byte数组用于存放图片字节数据
            byte[] buff = new byte[i];
            inputStream.read(buff);
            //记得关闭输入流
            inputStream.close();
            //设置发送到客户端的响应内容类型
            response.setContentType("image/*");
            OutputStream out = response.getOutputStream();
            out.write(buff);
            //关闭响应输出流
            out.close();
        } catch (Exception e) {
            // todo 打印日志
        }
        return "ok";
    }


    @ResponseBody
    @ApiOperation(value="根据ID删除资质", notes="根据ID删除资质")
    @RequestMapping(value = "/qualification/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deleteIndustry(@PathVariable Long id) {
        return qualificationService.deleteQualification(id);
    }
}
